<?php
/*
	订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$store_id=$this->session('store_id');
if(!$store_id){
	exi('请先选择门店！','error');
}
if($_RQ['op']=='list'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'store_id'=>$store_id,
		'order_type'=>1,
	);
	if($_RQ['status']=='2'){
		$params['status']=2;
		$return['params']['status']=2;
	}elseif($_RQ['status']=='4'){
		$params['status']=4;
		$return['params']['status']=4;
	}elseif($_RQ['status']=='5'){
		$params['status >=']=5;
		$return['params']['status >=']=5;
	}else{
		$params['status <=']=3;
		$return['params']['status <=']=3;
	}
	if($_RQ['ordersn']){
		$params['ordersn']=$_RQ['ordersn'];
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(isset($_RQ['is_pay'])){
		$params['is_pay']=intval($_RQ['is_pay']);
		$return['params']['is_pay']=$_RQ['is_pay'];
	}
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	$fields=array('id','mid','order_type','ordersn','price','status','pay_id','print_status','createtime','is_pay');
	$list=pdo_getslice('jy_dish_order',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$table=pdo_get('jy_dish_order_table',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$item['id'],'store_id'=>$store_id));
			$item['user_num']=$table['user_num'];
			$item['user_fee']=$table['user_fee'];
			$table=pdo_get('jy_dish_table',array('uniacid'=>$_SESSION['uniacid'],'id'=>$table['table_id']),array('title','group_id'));
			$tablegroup=pdo_get('jy_dish_table_group',array('uniacid'=>$_SESSION['uniacid'],'id'=>$table['group_id']),array('title'));
			$item['tablename']=$table['title']?$table['title'].'('.$tablegroup['title'].')':'无';
			$item['member']=pdo_get('core_members',array('id'=>$item['mid']),array('nickname','avatar'));
			$item['nickname']=$item['member']['nickname'];
			$item['avatar']=$item['member']['avatar'];
			$item['price']=$item['price'];
			$item['is_pay']=$item['is_pay'] ? '已付款':'未付款';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单列表'),
		),
		'export'=>'export',
		'export_title'=>'导出订单数据',
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'nickname','title'=>'顾客信息','type'=>'string'),
			array('name'=>'order_type','title'=>'订单类型','type'=>'radio','options'=>'order_types'),
			array('title'=>'餐桌/用餐人数','type'=>'img_string',
			'data'=>array(
				array('title'=>'餐桌：','name'=>'tablename'),
				array('title'=>'用餐人数：','name'=>'user_num','unit'=>'人')
				)
			),
			array('name'=>'price','title'=>'订单金额','type'=>'string'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'order_status','set'=>'set'),
			array('name'=>'is_pay','title'=>'是否已付款','type'=>'status_string'),
			array('name'=>'print_status','title'=>'打印状态','type'=>'radio','options'=>'print_statuss'),
			array('name'=>'createtime','title'=>'订单创建时间','type'=>'time'),
		),'action'=>array(
			array('op'=>'print','type'=>'post','title'=>'打印订单'),
			array('op'=>'detail','type'=>'href','title'=>'订单详情'),
		),),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'订单号','name'=>'ordersn','help'=>''),
			array('type'=>'select','width'=>'10','title'=>'订单状态','name'=>'status','help'=>'','options'=>'order_status2'),
			array('type'=>'select','width'=>'15','title'=>'付款状态','name'=>'is_pay','options'=>'pay_status2'),
			array('name'=>'createtime','width'=>'18','title'=>'订单创建时间','type'=>'time_range'),
		),
	);
	$return['htmls']=$htmls;
	set_html('framess',$this->menustore());
	$return['print_statuss']=$this->type__print_status();
	$return['is_pays']=array('0'=>'未支付','1'=>'已支付');
	$return['order_status']=$this->type__order_status_in();
	$return['order_types']=$this->type__order();
	$return['order_status2']=array(array('title'=>'待确认','id'=>0),array('title'=>'已确认','id'=>1),array('title'=>'制作中','id'=>2),array('title'=>'已上桌','id'=>3),array('title'=>'申请退款','id'=>4),array('title'=>'已退款','id'=>5),array('title'=>'已取消','id'=>6),array('title'=>'已关闭','id'=>7),array('title'=>'已完成','id'=>8));
	$return['order_types2']=array(array('title'=>'堂点','id'=>1),array('title'=>'外卖','id'=>2),array('title'=>'自取','id'=>3));
	$return['pay_status2']=array(array('title'=>'已付款','id'=>1),array('title'=>'未付款','id'=>0));

	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['status'])){
		$update['status']=intval($post['status']);
	}
	if($update){
		pdo_update('jy_dish_order',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('修改订单状态成功！');
}
if($_RQ['op']=='detail'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_dish_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		if($item['mid']){
			$member = pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
			$item['nickname'] = $member['nickname'];
			$item['mobile'] = $member['mobile'];
			$item['address'] = $member['address'];
		}else{
			$member = pdo_get('jy_dish_order_out',array('order_id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			$item['nickname'] = $member['receiver_name'];
			$item['mobile'] = $member['receiver_mobile'];
			$item['address'] = $member['receiver_address'];
		}
	}else{
		$item=array();
	}
	$goods=pdo_getall('jy_dish_order_goods',array('order_id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('goods_price','goods_package','goods_title'));
	foreach($goods as &$gv){
		$gv['name']=$gv['title'];
		if($gv['goods_package'] && $gv['goods_package']!='[]'){
			$gv['goods_package']=json_decode($gv['goods_package'],true);
			foreach($gv['goods_package'] as &$p){
					$gv['goods_title'] .= '['.$p['title'].'-￥'.$p['price'].'*'.$p['num'].']';
			}
		}
	}
	$table=pdo_get('jy_dish_order_table',array('order_id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$item['table']=$table;
	$item['goods']=$goods;
	$return['item']=$item;
	$return['print_statuss']=$this->type__print_status();
	$return['order_status']=$this->type__order_status_in();
	$return['order_types']=$this->type__order();
	$htmls=array(
		'type'=>'look',
		//'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回餐桌订单列表'),
		),
		'look'=>array(
					array('name'=>'nickname','title'=>'顾客信息','type'=>'string'),
					array('name'=>'mobile','title'=>'电话','type'=>'string'),
					array('name'=>'address','title'=>'地址','type'=>'string'),
					array('name'=>'order_type','title'=>'订单类型','type'=>'radio','options'=>'order_types'),
					array('name'=>'table.user_num','title'=>'用餐人数','type'=>'string','show'=>array('order_type'=>1)),
					array('name'=>'price','title'=>'订单金额','type'=>'string'),
					array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'order_status','set'=>'set'),
					array('name'=>'is_pay','title'=>'是否已付款','type'=>'switch'),
					array('name'=>'print_status','title'=>'打印状态','type'=>'radio','options'=>'print_statuss'),
					array('name'=>'createtime','title'=>'订单创建时间','type'=>'time'),
					array(
						'title'=>'商品详情',
						'type'=>'array',
						'name'=>'goods',
						'list'=>array(
							array('type'=>'string','name'=>'goods_title','title'=>'商品标题'),	
							array('type'=>'labelarray','name'=>'goods_price','title'=>'商品价格'),	
						),
				)
		)
	);
	$return['htmls']=$htmls;
	set_html('framess',$this->menustore());
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('请先选择订单！','error');
	}
	
	$update=array(
		'order_type'=>$post['order_type'],
		'price'=>$post['price'],
		'status'=>$post['status'],
		'is_pay'=>$post['is_pay'],
		'print_status'=>$post['print_status'],
	);
	if($update){
		pdo_update('jy_dish_order',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('修改订单成功！','','list');
}
if($_RQ['op']=='print'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择订单！','error');
	}
	$this->printmsg($text,$id,1);
	pdo_update('jy_dish_order',array('print_status'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('打印成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_dish_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('订单删除成功！');
}
if($_RQ['op']=='export'){
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'store_id'=>$store_id,
		'order_type'=>1,
	);
	if($_RQ['ordersn']){
		$params['ordersn']=$_RQ['ordersn'];
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if($_RQ['status']=='2'){
		$params['status']=2;
		$return['params']['status']=2;
	}elseif($_RQ['status']=='4'){
		$params['status']=4;
		$return['params']['status']=4;
	}elseif($_RQ['status']=='5'){
		$params['status >=']=5;
		$return['params']['status >=']=5;
	}else{
		$params['status <=']=3;
		$return['params']['status <=']=3;
	}
	if(isset($_RQ['order_type'])){
		$params['order_type']=intval($_RQ['order_type']);
		$return['params']['order_type']=$_RQ['order_type'];
	}
	if(isset($_RQ['is_pay'])){
		$params['is_pay']=intval($_RQ['is_pay']);
		$return['params']['is_pay']=$_RQ['is_pay'];
	}
	$fields=array('id','mid','order_type','ordersn','price','status','pay_id','print_status','createtime','is_pay');
	$list=pdo_getall('jy_dish_order',$params,$fields,'','createtime desc');
	if($list){
		foreach($list as &$item){
			$item['member']=pdo_get('core_members',array('id'=>$item['mid']),array('nickname','avatar'));
			$item['nickname']=$item['member']['nickname'];
			$item['avatar']=$item['member']['avatar'];
			$item['price']=$item['price'];
			$item['is_pay']=$item['is_pay'] ? '已付款':'未付款';
			$item['order_type']=$this->type__order()[$item['order_type']];
			$item['status']=$this->type__order_status_in()[$item['status']];
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
		}
	}
	$header=array(
		'ordersn'=>'订单号',
		'mid'=>'用户ID',
		'nickname'=>'昵称',
		'price'=>'订单价格',
		'is_pay'=>'是否支付',
		'order_type'=>'订单类型',
		'status'=>'订单状态',
		'createtime'=>'创建时间',
	);
	cfc('tools')->export2excel($header,$list,'订单数据-'.date('Y-m-d'));
}